package com.zdp.dubbo.consumer.service;

import com.zdp.dubbo.service.UserService;
import domain.User;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author ZhengDp
 * @Date 2023/6/3 17:04
 */
@Service
public class OrderService {

    @DubboReference
    UserService userService;

    public String createOrder(){
        User user = userService.getUser("1");
        System.out.println("创建订单");

        return user.toString()+" succeeded in creating the order";
    }

    public String createOrder1() throws InterruptedException {
        userService.sayHelloServerStream("Zhengdp", new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.out.println("接收到 服务端传递的数据 : " + data);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.out.println("服务端结束当前通信流");
            }
        });

        return "ZhengDp2";
    }

    public String createOrder2(){
        StreamObserver<String> response = userService.sayHelloClientStream(new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.out.println("服务端响应数据 : " + data);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

            }
        });
        response.onNext("客户端数据 data1");

        response.onNext("客户端数据 data2");

        return "ZhengDP2";
    }
}
